package com.hjxy.controller;

import com.github.pagehelper.PageInfo;
import com.hjxy.pojo.StudentAtt;
import com.hjxy.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

//考勤查询控制器（ztt）
@Controller
@RequestMapping("/select")
public class AttendanceController {
    @Autowired
    private AttendanceService attendanceService;

//    查询所有学生考勤
    @RequestMapping("/all")
    @ResponseBody
    public Map<String,Object> selectAll(){
        List<StudentAtt> list = attendanceService.selectAll();
        Map<String,Object> map = new HashMap<>();
        map.put("sa",list);
        return map ;
    }

//    查询个体学生考勤
    @RequestMapping("/one")
    @ResponseBody
    public List<StudentAtt> selectOne(){
        return null;
    }

//    分页查询的实现
    @GetMapping("/updatePage")
    @ResponseBody
    public Map<String,Object> updatePage(Integer currentPage,Integer pageSize){
        PageInfo<StudentAtt> pageInfo = attendanceService.updatePage(currentPage,pageSize);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("message", "获取用户数据成功");
        map.put("userList", pageInfo.getList()); // 返回查询出来的分页数据
        map.put("totalPage", pageInfo.getPages()); //返回当前总共有多少页
        return map;
    }

    //        从数据库获取模态框的默认数据
    @GetMapping("/getEdit")
    @ResponseBody
    public Map<String, Object> getEdit(Integer sno){
        StudentAtt studentAtt = attendanceService.getEdit(sno);
        Map<String,Object> map = new HashMap<>();
        if(studentAtt == null){
            map.put("code", -1);
            map.put("message", "获取用户数据失败");
        }else{
            map.put("code", 0);
            map.put("message", "获取用户数据成功");
            map.put("list", studentAtt);
        }
        return map;
    }

//    将编辑后的数据保存到数据库
    @PostMapping("/saveEdit")
    @ResponseBody
    public Map<String,Object> saveEdit(StudentAtt dataValue){
        StudentAtt s = attendanceService.getEdit(dataValue.getSno());
        Map<String, Object> map  = new HashMap<>();
        if (s!=null){
            int result = attendanceService.saveEdit(dataValue);
            map.put("rs",result);
        }else {
            int result = attendanceService.addEdit(dataValue);
            map.put("rs",result);
        }
        return map;
    }

    // 从数据库中删除
    @PostMapping("/removeEdit")
    @ResponseBody
    public Map<String,Object> removeEdit(Integer sno){
        Map<String, Object> map  = new HashMap<>();
        int rs = attendanceService.removeEdit(sno);
        map.put("rs",rs);
        return map;
    }
}
